package com.imooc.reader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imooc.reader.enity.Book;
import com.imooc.reader.enity.Evaluation;
import com.imooc.reader.enity.MemberReadState;
import com.imooc.reader.mapper.BookMapper;
import com.imooc.reader.mapper.EvaluationMapper;
import com.imooc.reader.mapper.MemberReadStateMapper;
import com.imooc.reader.service.BookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 图书服务
 */
@Service("bookService")
@Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
public class BookServiceImpl implements BookService {

    @Resource
    private BookMapper bookMapper;
    @Resource
    private MemberReadStateMapper memberReadStateMapper;
    @Resource
    private EvaluationMapper evaluationMapper;

    /**
     * 分布查询
     * @param categoryId 分类编号
     * @param order 排序方式
     * @param page
     * @param rows
     * @return 分布对象
     */
    @Override
    public IPage<Book> paging(Long categoryId,String order,Integer page, Integer rows) {
        Page<Book> bookPage = new Page<>(page, rows);
        QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
        if (categoryId!=null && categoryId!=-1){
            bookQueryWrapper.eq("category_id",categoryId);
        }
        if (order!=null){
            if (order.equals("quantity")){
                bookQueryWrapper.orderByDesc("evaluation_quantity");
            }else if(order.equals("score")){
                bookQueryWrapper.orderByDesc("evaluation_score");
            }
        }
        IPage<Book> pageObject = bookMapper.selectPage(bookPage,bookQueryWrapper );

        return pageObject;
    }

    /**
     * 返回书籍详细信息
     * @param bookId
     * @return
     */
    @Override
    public Book selectById(Long bookId) {

        return bookMapper.selectById(bookId);
    }

    /**
     * 更新图书评分/评价数量
     */
    @Override
    @Transactional
    public void updateEvaluation() {
        bookMapper.updateEvaluation();
    }

    /**
     * 新增图书
     *
     * @param book 新图书数据
     * @return 新增图书的数据
     */
    @Override
    @Transactional
    public Book createBook(Book book) {
        bookMapper.insert(book);
        return book;
    }

    /**
     * 更新图书
     *
     * @param book 新图书数据
     * @return 更新后的数据
     */
    @Override
    @Transactional
    public Book updateBook(Book book) {
        bookMapper.updateById(book);
        return book;
    }

    /**
     * 删除图书及相关数据
     *
     * @param bookId 图书编号
     */
    @Override
    @Transactional
    public void deleteBook(Long bookId) {
        bookMapper.deleteById(bookId);
        QueryWrapper<MemberReadState> mrsQueryWrapper = new QueryWrapper<MemberReadState>();
        mrsQueryWrapper.eq("book_id", bookId);
        memberReadStateMapper.delete(mrsQueryWrapper);
        QueryWrapper<Evaluation> queryWrapperEvaluation = new QueryWrapper<Evaluation>();
        queryWrapperEvaluation.eq("book_id", bookId);
        evaluationMapper.delete(queryWrapperEvaluation);
    }

}
